package com.jrh.biz.product;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import com.jrh.biz.feature.FeatureService;
import com.jrh.common.constant.JrhBizConstant;
import com.jrh.dal.ImgAdDao;
import com.jrh.dal.ProductDao;
import com.jrh.dal.ProductFeatureValueDao;
import com.jrh.domain.common.Page;
import com.jrh.domain.entity.Feature;
import com.jrh.domain.entity.FeatureValue;
import com.jrh.domain.entity.ImgAd;
import com.jrh.domain.entity.Product;
import com.jrh.domain.entity.ProductFeatureValue;

public class ProductServiceImpl implements ProductService {

    private ProductDao             productDao;
    private ProductFeatureValueDao productFeatureValueDao;
    private FeatureService         featureService;
    private ImgAdDao               imgAdDao;

    public void setFeatureService(FeatureService featureService) {
        this.featureService = featureService;
    }

    public void setImgAdDao(ImgAdDao imgAdDao) {
        this.imgAdDao = imgAdDao;
    }

    public void setProductDao(ProductDao productDao) {
        this.productDao = productDao;
    }

    public void setProductFeatureValueDao(ProductFeatureValueDao productFeatureValueDao) {
        this.productFeatureValueDao = productFeatureValueDao;
    }

    public List<Product> listByIds(List<Long> ids) {
        if (!CollectionUtils.isEmpty(ids)) {
            return productDao.listByIds(ids);
        }
        return new ArrayList<Product>();
    }

    public List<Product> listByIds(String[] ids) {
        if (ids == null || ids.length < 1) {
            return new ArrayList<Product>();
        }
        List<Long> list = new ArrayList<Long>();
        for (String id : ids) {
            try {
                list.add(Long.valueOf(id));
            } catch (Exception e) {
            }
        }
        return this.listByIds(list);
    }

    public List<Product> listByIds(String ids) {
        if (StringUtils.isBlank(ids)) {
            return new ArrayList<Product>();
        }
        String[] prudctIds = ids.split(",");
        return this.listByIds(prudctIds);
    }

    @Override
    public Page<Product> getProductInBrandByCateAndPSet(Long brandId, Long categoryId, Long psetId, Integer start,
                                                        Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("brandId", brandId);
        params.put("categoryId", categoryId);
        params.put("productSetId", psetId);
        return productDao.getProductDetailPageByCommonCondition(params, start, pageSize);
    }

    @Override
    public List<Product> getRecommendedProductInBrand(Long brandId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("brandId", brandId);
        params.put("recommended", JrhBizConstant.PRODUCT_RECOMMENDED);
        return productDao.listProductDetailByCommonCondition(params);
    }

    @Override
    public Page<Product> getAllProductInBrand(Long brandId, Integer start, Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("brandId", brandId);
        return productDao.getProductDetailPageByCommonCondition(params, (start - 1) * pageSize, pageSize);
    }

    @Override
    public boolean updateProductStatus(Product product, boolean enable) {
        if (product == null) {
            return false;
        }
        String status = enable ? JrhBizConstant.PRODUCT_STATUS_ENABLE : JrhBizConstant.PRODUCT_STATUS_DISABLE;
        product.setStatus(status);
        return productDao.updateProductBySale(product);
    }

    @Override
    public boolean updateProductBySale(Product product) {
        productDao.updateProductBySale(product);

        imgAdDao.deleteByRef(product.getId(), ImgAd.AD_TYPE_PRODUCT);
        productFeatureValueDao.delete(product.getId());
        updatePreductRef(product);
        return true;
    }

    private void updatePreductRef(Product product) {
        if (product.getFeatureValueList() != null) {
            for (FeatureValue fv : product.getFeatureValueList()) {
                ProductFeatureValue pfv = new ProductFeatureValue();
                pfv.setProductId(product.getId());
                pfv.setFeatureValueId(fv.getId());
                productFeatureValueDao.create(pfv);
            }
        }
        if (product.getImgAds() != null) {
            for (ImgAd imgAd : product.getImgAds()) {
                imgAd.setRefId(product.getId());
                imgAd.setRefType(ImgAd.AD_TYPE_PRODUCT);
                imgAdDao.create(imgAd);
            }
        }
    }

    @Override
    public boolean updateProductByUser(Product product) {
        return productDao.updateProductByUser(product);
    }

    @Override
    public void deleteProduct(Long productId) {
        productDao.delete(productId);
    }

    @Override
    public Product getProductById(Long productId) {
        Product product = productDao.load(productId, null);
        if (product == null) {
            return null;
        }
        List<Feature> featureList = featureService.listFeatureDetailByProduct(product.getId());
        List<ImgAd> list = imgAdDao.listByCondition(new ImgAd(productId, ImgAd.AD_TYPE_PRODUCT));
        product.setFeatureList(featureList);
        product.setImgAds(list);
        return product;
    }

    @Override
    public Product getAvailableProductById(Long productId) {
        Product product = productDao.load(productId, JrhBizConstant.PRODUCT_STATUS_ENABLE);
        if (product == null) {
            return null;
        }
        List<Feature> featureList = featureService.listFeatureDetailByProduct(product.getId());
        List<ImgAd> list = imgAdDao.listByCondition(new ImgAd(productId, ImgAd.AD_TYPE_PRODUCT));
        product.setFeatureList(featureList);
        product.setImgAds(list);
        return product;
    }

    @Override
    public void createProduct(Product product) {
        productDao.create(product);
        if (product.getFeatureValueList() != null) {
            for (FeatureValue fv : product.getFeatureValueList()) {
                ProductFeatureValue pfv = new ProductFeatureValue();
                pfv.setProductId(product.getId());
                pfv.setFeatureValueId(fv.getId());
                productFeatureValueDao.create(pfv);
            }
        }
        if (product.getImgAds() != null) {
            for (ImgAd imgAd : product.getImgAds()) {
                imgAd.setRefId(product.getId());
                imgAd.setRefType(ImgAd.AD_TYPE_PRODUCT);
                imgAdDao.create(imgAd);
            }
        }
    }

    @Override
    public Page<Product> getProductByCateStyleAndFeature(Long cateId, Long styleId,
                                                         List<FeatureValue> featureValueList, Integer start,
                                                         Integer pageSize) {
        return null;
    }

    @Override
    public List<Product> listProductByCondition(Long brandId, String name) {
        return productDao.listProductByCondition(brandId, name);
    }

    @Override
    public List<Product> listProductByProductRef(Long refId, String refType) {
        return productDao.listProductByProductRef(refId, refType);
    }

}
